var allpro= null;
var voltage_type=null;
var gt_type=null;
var gt_material=null;
var type1 = ["杆塔", "柱上变压器", "搭火点", "下户点", "表箱"];
var type2 = ["变压器", "箱式变电站", "中压开关柜", "柱上开关", "无功补偿", "低压开关柜", "电缆分接箱"];
var userall = null;
$(function () {

    $.when(
        $.get(baseURL + 'bss/apbase/select?ourid=isok', function (r) {
            allpro = r.returnlist;
            vm.allpro=allpro;

        }),
        getdatadictionary("voltage_type"),
        getdatadictionary("gt_type"),
        getuserall(),
        getdatadictionary("gt_material")

    ).done(
        $("#jqGrid1").jqGrid({
            url: baseURL +'bss/apbase/list?state=isdelno&ourid=isok',
            datatype: "json",
            colModel: [
                { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden: true  },

                { label: '项目名称', name: 'pBName', index: 'p_b_name', width: 200 },
                { label: '参与人', name: 'pBCollectid', index: 'p_b_collectid', width: 150 ,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";


                        var ss = (cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "") + "").split(",");
                        for (var i = 0; i < ss.length; i++) {

                            for (d in userall) {
                                if (ss[i] == userall[d].userId + "") {

                                    names += userall[d].username + ",";
                                }
                            }
                        }
                        if (names.length > 0) {
                            names = names.substring(0, names.length - 1);
                        }
                        return names;
                    }
                },

                { label: '创建时间', name: 'pBAddtime', index: 'p_b_addtime', width: 150,
                    formatter:function(value,options,row){
                        return value!=null?new Date(value).Format('yyyy-MM-dd hh:mm'):"";}
                }
            ],
            viewrecords: true,
            height: 500,

            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager1",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid1").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        }),
    $("#jqGrid").jqGrid({
        url: baseURL + 'bss/dpoint/list',
        datatype: "json",
        colModel: [
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true  , hidden: true},
			{ hidden: true,label: '所属项目', name: 'pGuid', index: 'p_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {

                    for (d in allpro) {

                        if (cellvalue == allpro[d].guid) return allpro[d].pBName;
                    }
                }
			},
			{ label: '工作类型', name: 'pPWorktype', index: 'p_p_worktype', width: 80 },
			// { label: '所属项目范围ID', name: 'pRGuid', index: 'p_r_guid', width: 80 },
			{ label: '点位名称', name: 'pPName', index: 'p_p_name', width: 80 },
			{ label: '点位编号', name: 'pPCode', index: 'p_p_code', width: 80 },
			// { label: '上联点ID', name: 'pPUpguid', index: 'p_p_upguid', width: 80 },
			{ label: '档距', name: 'pPInputspan', index: 'p_p_inputspan', width: 80 },
			{ hidden: true, label: '杆塔类型 ', name: 'pPPropertyone', index: 'p_p_propertyone', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in gt_type) {
                        if (cellvalue == gt_type[d].dKey) return gt_type[d].dValue;
                    }
                }
			},
			// { label: '表箱位数 数据字典', name: 'pPPropertytwo', index: 'p_p_propertytwo', width: 80 },
			{ hidden: true, label: '杆高', name: 'pPBar', index: 'p_p_bar', width: 80 },
			{ label: '电压等级', name: 'pPVoltage', index: 'p_p_voltage', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in voltage_type) {
                        if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                    }
                }
			},
			// { label: '接线方式 数据字典', name: 'pPPropertythree', index: 'p_p_propertythree', width: 80 },
			{  hidden: true,label: '下户数', name: 'pPHucount', index: 'p_p_hucount', width: 80 },
			{ hidden: true, label: '角度', name: 'pPAngle', index: 'p_p_angle', width: 80 },
			// { label: '导线材料ID', name: 'pWGuid', index: 'p_w_guid', width: 80 },
			{ hidden: true, label: '导线回数', name: 'pPBack', index: 'p_p_back', width: 80 },
			{  hidden: true,label: '导线线数', name: 'pPLinenum', index: 'p_p_linenum', width: 80 },
			{ hidden: true, label: '是否接地', name: 'pPIsearth', index: 'p_p_isearth', width: 80 },
			{ hidden: true, label: '拉线数', name: 'pPWirenum', index: 'p_p_wirenum', width: 80 },
			{ label: '设备数量', name: 'deviceCount', index: 'deviceCount', width: 80 },
			// { label: '高程', name: 'pPAltitude', index: 'p_p_altitude', width: 80 },
			// { label: '高差', name: 'pPHighbad', index: 'p_p_highbad', width: 80 },
			// { label: '采集人', name: 'pPUserid', index: 'p_p_userid', width: 80 },
			{ label: '采集时间', name: 'pPCollecttime', index: 'p_p_collecttime', width: 80,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";}
			},
			// { label: '数据状态', name: 'pPDatastate', index: 'p_p_datastate', width: 80 },
			{ hidden: true,label: '经度', name: 'pPInputlongitude', index: 'p_p_inputlongitude', width: 80 },
			{ hidden: true,label: '纬度', name: 'pPInputlatitude', index: 'p_p_inputlatitude', width: 80 },
            { label: '操作', name: '', width: 100, formatter: function (cellvalue, options, rowObject) {

                return "<a  class='btn btn-primary' onclick='showpointInfo(\"" + rowObject.pPName +rowObject.pPCode+ "\",\"" + rowObject.guid + "\",\"" + rowObject.pGuid + "\")' >&nbsp;点位信息</a>"
            }
            }
        ],
		viewrecords: true,
        height: 500,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    })



    )
});
function getdatadictionary(sss)
{

    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="voltage_type")
        {
            voltage_type=r.returnlist;
        }
        if(sss=="gt_type")
        {
            gt_type=r.returnlist;
        }
        if(sss=="gt_material")
        {
            gt_material=r.returnlist;
        }

        vm.voltage_type=voltage_type;
        vm.gt_type=gt_type ;
        vm.gt_material=gt_material;


    });
}
function  getuserall()
{
    $.get(baseURL + "sys/user/select" , function (r) {
        userall = r.returnlist;

    });
    vm.userall=userall;

}
function showpointInfo(pname,pdguid,pguid) {
    var title = pname+"点位信息";
    var content = "dpointothor.html?pdguid=" + pdguid+"&pguid=" + pguid;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['95%', '95%'],
        content: content

    });
}


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showdg: true,
        userall:userall,
		title: null,
        voltage_type:voltage_type,
        gt_type:gt_type,
        gt_material:gt_material,
        allpro: allpro,
        pType: type1,
        pRange: {},
        q:{
            psname:null,
            ppppBName:null,
            pGuid:null
        },
		dPoint: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dPoint = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;

            vm.title = "修改";

            vm.getInfo(guid)

                vm.voltage_type=voltage_type;
                vm.gt_type=gt_type ;
            vm.gt_material=gt_material;
		},
		saveOrUpdate: function (event) {
			var url = vm.dPoint.guid == null ? "bss/dpoint/save" : "bss/dpoint/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dPoint),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/dpoint/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/dpoint/info/"+guid, function(r){
                vm.dPoint = r.dPoint;
                vm.dPoint.pPCollecttime=  r.dPoint.pPCollecttime !=null?new Date( r.dPoint.pPCollecttime ).Format('yyyy-MM-dd'):"";
              if(vm.dPoint.pPName=="杆塔")
              {
                    vm.showdg = false;
                }
                else
              {
                  vm.showdg = true;
              }

            });
		},
        getBasic: function (guid) {
            console.log(guid)

            for (d in  allpro) {
                if (allpro[d].guid == guid) {

                    if (allpro[d].pBType == '100')
                        vm.pType = type1;
                    else
                        vm.pType = type2;
                }
            }
            console.log("aaaa"+vm.pType);
            $.get(baseURL + "bss/aprange/infoList/" + guid, function (r) {
                console.log(r);
                vm.pRange = r.aPRangeList;
                console.log(vm.pRange);
            });

        },
        showprolist:function (event) {

     // vm.q.pGuid='b6400318-7a6f-4de5-92e8-ddec55f10394';
           layer.open({
                    type: 1,
                    offset: '50px',
                    skin: 'layui-layer-molv',
                    title: "选择项目",
                    area: ['700px', '400px'],
                    shade: 0,
                    shadeClose: false,
                    content: jQuery("#deptLayermy"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var grid = $("#jqGrid1");
                        var rowKey = grid.getGridParam("selrow");
                        if (!rowKey) {
                            alert("请选择一条记录");
                            return;
                        }

                       var selectedIDs = grid.getGridParam("selarrrow");
                        if (selectedIDs.length > 1) {
                            alert("只能选择一条记录");
                            return;
                        }
                      vm.getBasic(selectedIDs[0]);
                      vm.q.pGuid=selectedIDs[0];

                        for (d in  allpro) {
                            if (allpro[d].guid == selectedIDs[0]) {
                                vm.q.psname=allpro[d].pBName;
                            }
                        }
                        layer.close(index);

                    }
                });


        },
		reload: function (event) {

			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'pGuid': vm.q.pGuid,
                    'pPCode': vm.q.pPCode,
                    'pType': vm.q.pType,
                    'voltage': vm.q.voltage,
                    'range': vm.q.range
                },
                page:page
            }).trigger("reloadGrid");
		},
        reloadpro: function (event) {

         console.log("a_______");
            var page = $("#jqGrid1").jqGrid('getGridParam','page');
            $("#jqGrid1").jqGrid('setGridParam',{
                postData:{
                    'pBName': vm.q.ppppBName,
                },
                page:page
            }).trigger("reloadGrid");
        }

	}
});
